<template>
  <view class="bgColor" v-if="update">
    <view style="display: flex">
      <view style="flex: 9">
        <uni-card :title="order.userAddress.recipient" @click="addressFun" :extra="order.userAddress.phone">
          <text class="uni-body">{{ order.userAddress.detailedAddress }}</text>
        </uni-card>
      </view>
    </view>

    <view style="margin-top: 30rpx;width: 90%;display: flex;background-color: white;margin: auto;border-radius: 20rpx;padding-top: 30rpx;padding-bottom: 30rpx;padding-right: 20rpx;padding-left: 20rpx">
      <view style="flex: 1">
        <u-image radius="10" :src="order.commodity.imageUrl" width="170" height="170"></u-image>
      </view>
      <view style="flex: 3;padding-left: 20rpx">
        <view style="width: 100%;height: 80%;padding: 10rpx">
          <text style="font-weight: 600;font-size: 28rpx" class="textTile">{{order.commodity.description}}</text>
        </view>

        <view style="width: 100%;position: relative;height: 20%;padding-left: 5rpx">
          <text style="font-size: 26rpx;color: rgb(154,152,152);margin-right: 20rpx;position: absolute;bottom: 20rpx;background-color: #D7D7D7FF;padding:6rpx 10rpx">{{order.commodity.specification}}</text>
        </view>

      </view>
    </view>

    <view class="fix-bottom" style="width: 100%"  @click="changeAddress">
      <button type="primary">提交修改</button>
    </view>
  </view>
</template>

<script>
import {mainService} from "../../../api/buyer/main";

export default {
  name: "ChangeAddress",
  data() {
    return {
      user:{},
      addresses:[],
      order:{},
      update:true,
    }
  },
  created(){
    // this.user = uni.getStorageSync('userInfo')
    // if (!this.user){
    //   //未登录
    //   // uni.showToast({
    //   //   title: '未登录，请去登录',
    //   //   icon: 'none'
    //   // });
    //   //成功后延迟1秒跳转
    //   setTimeout(() => {
    //     uni.navigateTo({
    //       url:"/pages/public/login/login",
    //       success:res =>{},
    //       fail:() =>{},
    //       complete:() => {}
    //     });
    //   }, 1000);
    // }else{
    //
    //   this.order.userId=this.user.eid
    // }
  },
  onLoad(option) {
    const s = decodeURIComponent(option.order);
    this.order = JSON.parse(s)

  },
  onShow() {
    let that = this
    uni.$on('updateSelectAddress',function(selectAddress){
      that.order.userAddress=selectAddress;
      that.order.userAddressId=selectAddress.eid;
      that.order.recipient=selectAddress.recipient;
      that.order.phone=selectAddress.phone;
      that.order.detailedAddress=selectAddress.area+selectAddress.detailedAddress;
      that.reload();
    })
  },
  methods:{
    reload(){
      this.update=false
      this.update=true
    },
    init(){
      mainService.getDefaultAddress().then((res)=>{
        this.order.userAddress=res.data
        this.order.userAddressId=res.data.eid
        this.reload();
        console.log(res.data)
      })
    },

    addressFun(){
      uni.navigateTo({
        url: '/pages/buyer/public/Address',
        // url: '/pages/staff/application/production/taskList/task-detail'
      })
    },
    changeAddress(){
      uni.showLoading({title: '加载中',mask:true});
      mainService.changeAddress(this.order).then((res)=>{
        uni.hideLoading();
        uni.showToast({
          title: '修改地址成功',
          duration: 2000
        });
        this.back()
      }).catch((err)=>{
        uni.hideLoading();
        uni.showToast({
          title: '修改地址失败',
          duration: 2000
        });
      })
    },
    back(){
      uni.navigateBack({
        delta:1,//返回层数，2则上上页
      })
    },
  }
}
</script>

<style scoped>
.textTile{
  overflow: hidden;
  -webkit-line-clamp: 2;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
}
</style>